package com.company.beauty.common.async;

import com.company.beauty.common.handler.SimpleAsyncUncaughtExceptionHandler;
import com.company.nuwa.common.handler.ThreadPoolHandler;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

/**
 * <p>async configurer support</p >
 *
 * @author youyoukele
 * @version 1.0
 * @date 2022/9/27
 */
@EnableAsync
public class AsyncConfigurerSupport implements AsyncConfigurer {

    @Override
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor threadPoolTaskExecutor = ThreadPoolHandler.build("default-task-", 60, 200,
                Runtime.getRuntime().availableProcessors(),
                Runtime.getRuntime().availableProcessors() * 2,
                ThreadPoolHandler.RejectedHandlerStrategy.CALLER);

        threadPoolTaskExecutor.initialize();
        return threadPoolTaskExecutor;
    }

    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return new SimpleAsyncUncaughtExceptionHandler();
    }

}
